package com.swift.base;


import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.StrictMode;
import android.util.DisplayMetrics;
import android.view.Display;

import com.swift.utils.DeviceUtil;

import org.litepal.LitePal;
import org.xutils.DbManager;
import org.xutils.DbManager.DaoConfig;
import org.xutils.DbManager.DbOpenListener;
import org.xutils.DbManager.DbUpgradeListener;
import org.xutils.ImageManager;
import org.xutils.x;

public  class ApplicationBase extends Application {

	public static DbManager DbContext;
	public static SharedPreferences LocalData;
	public static ImageManager XBitmap;
	public static Context Context; 

	protected String LocalDataPath = "local_data";
	protected String DB_NAME = "swift.db3";
	
	@Override
	public void onCreate() {
		//在 android3.0及其以上的版本都会强制 要求我们在单独的线程中使用访问网络。
		//对此我们要忽略这个强制的策略，我们要添加以下代码
		super.onCreate();
		LitePal.initialize(this);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);

		x.Ext.init(this);
		Context = getBaseContext();
		createDatabase(Context, DB_NAME);
		LocalData = this.getSharedPreferences(LocalDataPath, MODE_PRIVATE);
		XBitmap = x.image();
		DeviceUtil.getInstance().init(this);
	}

	
	
	public static void createDatabase(Context context, String dbName) {
		DaoConfig config = new DaoConfig();
		config.setDbName(dbName);
		config.setDbVersion(1);
		config.setDbOpenListener(new DbOpenListener() {
            @Override
            public void onDbOpened(DbManager db) {
                // 开启WAL, 对写入加速提升巨大
                db.getDatabase().enableWriteAheadLogging();
            }
		});
		config.setDbUpgradeListener(new DbUpgradeListener() {
            @Override
            public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
            }
        });
		DbContext = x.getDb(config);
	}
	
	public static void reCreateDatabase(Context context){
		String dbName =DbContext.getDaoConfig().getDbName();
		try {			
			DbContext.dropDb();
			DbContext.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		createDatabase(context, dbName);
	}

	public static int getScreenWidth(Activity context) {
		Display display = context.getWindow().getWindowManager()
				.getDefaultDisplay();
		DisplayMetrics displayMetrics = new DisplayMetrics();
		display.getMetrics(displayMetrics);
		return displayMetrics.widthPixels;
	}
	
	public static boolean checkNetworkAvailable() {  
        ConnectivityManager manager = (ConnectivityManager)Context.getSystemService(CONNECTIVITY_SERVICE);  
        //去进行判断网络是否连接  
        if(manager != null){
	         NetworkInfo[] info = manager.getAllNetworkInfo();
	         if(info != null){
				  for(int i=0;i<info.length; i++){
					  if(info[i].getState() == NetworkInfo.State.CONNECTED){  //判断网络是否已连接
						  return true;  //网络已连接
					  }
				  }
	         }
        }
        return false;
	}
}
